package com.topisv.tms.web.occupiedAreaConfig;

import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.JSONParam;
import com.topisv.tms.entity.occupiedArea.StockTypeCostConfig;
import com.topisv.tms.entity.occupiedArea.StockTypeMapping;
import com.topisv.tms.entity.stock.CusStock;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.account.ShiroDbRealm.ShiroUser;
import com.topisv.tms.service.occupiedArea.StockTypeCostConfigService;
import com.topisv.tms.tools.DataTablesUtils;
import com.topisv.tms.utils.enums.StatusEnums;
import com.topisv.tms.web.base.BaseController;

/**
 * 库位类型成本配置_control
 * @author ch
 */
@Controller
@RequestMapping({ "/stockTypeCostConfig" })
public class StockTypeCostConfigController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(StockTypeCostConfigController.class);

	@Autowired
	private StockTypeCostConfigService stccService;
	@Autowired
	private AreaGroupService areaGroupService;
	/**
	 * 配置查询页面
	 */
	@RequestMapping(value = "v2_stockTypeCostConfigPage")
	public String stockTypeCostConfigPage(Model model, ServletRequest request,
			ServletResponse response) {
		List<AreaGroup> warehouseList =  findWarehouseList();
		model.addAttribute("warehouseList",warehouseList);
		return "/stockTypeCostConfig/stockTypeCostConfigPage";
	}
	public List<AreaGroup> findWarehouseList(){
		ShiroUser curUser = getShiroUser();
		List<AreaGroup> warehouseList = null;
		if (StatusEnums.user_type_customer.getStatus().equals(curUser.userType)) {
			warehouseList = areaGroupService.loadAreaGroupByAuthority(curUser, curUser.customerCode);
		} else if (StatusEnums.user_type_paiu.getStatus().equals(curUser.userType)) {
			warehouseList = areaGroupService.findByAreaGroups(curUser.areagroups);
		}
		return warehouseList;
	}
	
	@RequestMapping(value = "findStockTypeCostConfig")
	public void findStockTypeCostConfig(@RequestBody JSONParam[] paramJson,HttpServletRequest request, 
			HttpServletResponse response) {
		JSONObject json=new JSONObject();
		Page<StockTypeCostConfig> page=null;
		PrintWriter out=null;
		try{
			//变量定义
			out=response.getWriter();
			
			//客户端参数
			Map<String, String> paramMap = DataTablesUtils.convertToMap(paramJson);
			//查询
			String sEcho = paramMap.get("sEcho");  
			int start = Integer.parseInt(paramMap.get("iDisplayStart"));
			int pageSize = Integer.parseInt(paramMap.get("iDisplayLength")); 
			int pageNumber=(start/pageSize)+1;
			
			String warehouseCode = paramMap.get("warehouseCode");
			StockTypeCostConfig config = new StockTypeCostConfig();
			config.setWarehouseCode(warehouseCode);
			page = stccService.findTable(config, pageNumber, pageSize);
			List<StockTypeCostConfig> stccList = page.getContent();

			//封装查询结果
	        json.put("aaData", stccList);
	        json.put("sEcho", sEcho);
	        json.put("iTotalRecords", page.getTotalElements());
	        json.put("iTotalDisplayRecords", page.getTotalElements());
		} catch (Exception e) {
			log.error("查询异常",e);
			json.put("status", "0");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	
	@RequestMapping(value = "ajaxSave")
	public void ajaxSave(@ModelAttribute("vo")StockTypeCostConfig stcc,Model model, ServletRequest request,
			ServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			ShiroUser curUser = getShiroUser();
			stcc.setCreateBy(curUser.getJobuuid());
			stcc.setCreateTime(new Date());
			stccService.save(stcc);
			
			json.put("msg", "success");
			json.put("content", "保存成功");
		} catch (NumberFormatException e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		} catch (Exception e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "库位类型编码不能重复");
		}
		out.print(json.toString());
	}
	
	@RequestMapping(value = "findById")
	public void findById(@RequestParam(value = "id") String id,Model model, ServletRequest request,
			ServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			StockTypeCostConfig stcc = stccService.findById(Long.valueOf(id));
//			List<AreaGroup> warehouseList =  findWarehouseList();
			
			json.put("stcc", stcc);
//			json.put("warehouseList", warehouseList);
			json.put("msg", "success");
			json.put("content", "删除成功");
		} catch (NumberFormatException e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		} catch (Exception e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		}
		fillSelectValue(model, null);
		out.print(json.toString());
	}
	
	@RequestMapping(value = "deleteStockTypeCostConfig")
	public void deleteStockTypeCostConfig(@RequestParam(value = "id") String id,Model model, ServletRequest request,
			ServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			stccService.delete(Long.valueOf(id));
			
			json.put("msg", "success");
			json.put("content", "删除成功");
		} catch (NumberFormatException e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		} catch (Exception e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		}
		fillSelectValue(model, null);
		out.print(json.toString());
	}
	
	
	
	@RequestMapping(value = "findMappingById")
	public void findMappingById(@RequestParam(value = "id") String id,Model model, ServletRequest request,
			ServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			StockTypeCostConfig stcc = stccService.findById(Long.valueOf(id));
//			List<AreaGroup> warehouseList =  findWarehouseList();
			List<CusStock> unAssignList = stccService.findUnassignStockType(stcc);
			List<StockTypeMapping> assignList = stccService.findAssignStockType(stcc);
			
			
			json.put("unAssignList", unAssignList);
			json.put("assignList", assignList);

			json.put("status", "success");
			json.put("content", "查询成功");
		} catch (NumberFormatException e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		} catch (Exception e) {e.printStackTrace();
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		}
		fillSelectValue(model, null);
		out.print(json.toString());
	}
	
	@RequestMapping(value = "saveMapping")
	public void saveMapping(@RequestParam(value = "assignStockCodes") String assignStockCodes,
			@RequestParam(value = "stockConfigId") String stockConfigId,Model model, ServletRequest request,
			ServletResponse response) {
		PrintWriter out=null;
		JSONObject json=null;
		try {
			out=response.getWriter();
			json=new JSONObject();
			
			StockTypeCostConfig stcc = stccService.findById(Long.valueOf(stockConfigId));
			if(stcc==null){
				json.put("msg", "fail");
				json.put("content", "数据异常");
				out.print(json.toString());
				return;
			}
			stccService.saveMapping(assignStockCodes,stcc);
			

			json.put("msg", "success");
			json.put("content", "查询成功");
		} catch (NumberFormatException e) {
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		} catch (Exception e) {e.printStackTrace();
			log.error("查询异常",e);
			json.put("msg", "fail");
			json.put("content", "服务器异常");
		}
		out.print(json.toString());
	}
	
	
}


